# --------------------------------------------------------------------------- #
# This is the main makefile for the UVAS framework application.               #
# It should run without problem on Linux, MinGW and Cygwin.                   #
# It needs CppUnit, the Verse API and pthread to compile.                     #
# --------------------------------------------------------------------------- #

# path variables
LOCALINCPATH := /usr/local/include
LOCALLIBPATH := /usr/local/lib
LUAINC := /usr/include/lua50

# project variables
VERSION := $(shell cat version.txt)
PROJPATH := $(shell pwd)
SUBDIRS := . kernel util debug geometry scenegraph domain bsp simulation simulation/beamtracing simulation/perceptual frontend soundrendering sgiglu gpc
CSUBDIRS := util
TESTSUBDIRS := $(addsuffix /test,$(SUBDIRS))
ALLSUBDIRS := $(SUBDIRS) $(TESTSUBDIRS)
SRCS := $(wildcard $(addsuffix /*.cpp,$(ALLSUBDIRS)))
CSRCS := $(wildcard $(addsuffix /*.c,$(ALLSUBDIRS)))
HDRS := $(wildcard $(addsuffix /*.h,$(ALLSUBDIRS)))
OBJS := $(SRCS:.cpp=.o) $(CSRCS:.c=.o)
BIN := uvas
INSTALLDIR := /usr/local

#OBJS += ../gpc/gpc.o


# build variables
MAKE := make
export CC := gcc
export CINCS = -I$(PROJPATH) -I$(LOCALINCPATH)
export CFLAGS = $(CINCS) -Wimplicit -Wreturn-type -g

export CXX := g++
export CXXINCS = $(CINCS) -I$(LUAINC)
export CXXFLAGS = $(CXXINCS) -D__TBSPTREE__ -fexceptions -Wimplicit -Wreturn-type -g
#export CXXFLAGS = $(CXXINCS) -D_TRACE=20 -D_TEST -D__DEBUG__ -D__PROFILE__ -D__TBSPTREE__ -fexceptions -Wimplicit -Wreturn-type -g

# link variables
LD = $(CXX)
LDFLAGS = -L$(LOCALLIBPATH)
LDLIBS = -lcppunit -lverse -L uvsrp -luvsrp  
#-ltolua -llualib -llua 

# check for cygwin or mingw
CYGWIN := $(shell gcc --version | grep -c cyg)
MINGW := $(shell gcc --version | grep -c mingw)

# do cygwin, mingw and linux specific stuff
ifneq ($(CYGWIN), 0) # cygwin
LDLIBS +=  -lwinmm -lws2_32 -lpthread -lopengl32 -lglut32 -lglu32
else
ifneq ($(MINGW), 0) # mingw
LDLIBS += -lwinmm -lws2_32 -lpthreadGCE -lopengl32 -lglut32 -lglu32
else # linux
LDLIBS += -lpthread -lGL -lGLU -lglut
endif
endif

.PHONY: all all-before all-after clean clean-custom subdirs tester uvsrp $(ALLSUBDIRS)

all: all-before $(BIN) all-after

all-before: uvsrp

subdirs: $(ALLSUBDIRS)

tester:
	if ! test -f test/uvastester.cpp; then cp test/uvastester.cpp.default test/uvastester.cpp; fi

$(ALLSUBDIRS):
	if ! test -d $@; then mkdir $@;fi
	if ! test -f $@/Makefile.mf; then cp Makefile.subdirs $@/Makefile.mf; fi
	$(MAKE) -C $@ -f Makefile.mf $(MAKECMDGOALS)

$(BIN): tester subdirs
	$(LD) -o $@ $(OBJS) $(LDFLAGS) $(LDLIBS)

clean: clean-custom subdirs
	rm -f $(OBJ) $(BIN) *~ 

dist: $(SRCS) $(HDRS) Doxyfile Makefile Makefile.subdirs version.txt
	tar czf uvas-$(VERSION).src.tar.gz $^

doc: $(HDRS) Doxyfile
	doxygen

uvsrp: 
	$(MAKE) -C uvsrp -f Makefile

uvsrp_install: 
	$(MAKE) -C uvsrp -f Makefile install

install:
	cp uvas $(INSTALLDIR)/bin/
	$(MAKE) -C uvsrp -f Makefile install
